<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="所属租户:" name="tenantId" style='width: 250px;'>
              <a-select
                show-search
                v-model:value="where.tenantId"
                placeholder="请选择所属租户"
                style="width: 200px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option v-for="item in tenantList" :key="item.tenantId" >
                  {{ item.companyName }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item label="手机号:" style="margin-left: 20px;">
              <a-input v-model:value.trim="where.phone" placeholder="请输入手机号" allow-clear />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="tmapId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          v-model:selection="selection"
          :scroll="{ x: 'max-content' }"
        >

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>

    <!-- 编辑弹窗 -->
    <usermapping-edit v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
  </div>
</template>

<script>
import { UsermappingApi } from '@/api/tenantdata/UsermappingApi';
import UsermappingEdit from './usermapping_edit.vue';
import { SysTenantApi } from '@/api/tenant/SysTenantApi';
import { message } from 'ant-design-vue';

export default {
  name: 'Usermapping',
  components: {
    //PlusOutlined,
    UsermappingEdit
  },
  async mounted() {
    SysTenantApi.list({}).then(resp => {
      if (resp && resp.data &&  resp.data.length > 0) {
        this.tenantList = resp.data;
      }
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '账号',
          dataIndex: 'account'
        },
        {
          title: '所属租户',
          dataIndex: 'companyName',
        },
        {
          title: '租户编码',
          dataIndex: 'tenantCode',
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          key: 'action',
          width: 280,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      tenantList : []
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/05/02 22:07
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/05/02 22:07
     */
    reset() {
      this.where.tenantId = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2022/05/02 22:07
     */
    async remove(row) {
      message.warn("请登录到具体租户去删除用户");
      return;
      //const result = await UsermappingApi.delete({ tmapId: row.tmapId });
      //message.success(result.message);
      //this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2022/05/02 22:07
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return UsermappingApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
